package com.stdspringboot.config;

import com.stdspringboot.interceptor.LoginInter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//配置类必须加此注解，否则不生效
@Configuration
public class SpringBootConfig implements WebMvcConfigurer {

    @Resource
    private LoginInter loginInter;

    //添加拦截器，通过形参注册拦截器
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInter()) // 自行创建的对象，可行，但不推荐
//        registry.addInterceptor(loginInter) // 推荐使用 springboot同一管理创建，并注入
//                .addPathPatterns("/**")  // 拦截所有路径
//                .excludePathPatterns(  //登录页面、注册页面、js、登录、注册的controller都不拦截
//                        "/login3.html",
//                        "/reg2.html",
//                        "/js/**",
//                        "/login",
//                        "/reg",
//                        "/noLogin",
//                        "/favicon.ico");
    }


    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

}
